<?php
class Post_IndexController extends Zendvn_Controller_Action{
	
	public function init()
	{
		parent::init();
		
	
		$this->view->main = TRUE;
		
	
		
		$storage = new Zend_Auth_Storage_Session();
		$data = $storage->read();
		/*
		if (isset($data))
		{
			
		}
		else 
		{
			$this->_redirect('/registration/auth/login');
		}*/
	}
	
	public function indexAction(){

		$this->view->module = "profile";
		$id = $this->_request->getParam('id');
		$pid = $this->_request->getParam('pid');
		
		$this->view->user_wall_id = @$_GET["id"];
		
		if($this->getRequest()->isPost()){			
			
			$storage = new Zend_Auth_Storage_Session();
			$data = $storage->read();
			
			$module = $_POST["module"];
			
			$this->view->status = $module;
			
			
			if($module == "preview"){

				// Tao bai post
				$title = $_POST["title"];
				$content = $_POST["content"];
				$idcategory = $_POST["idcategory"];
				$status = "preview";
				
				
				
				$post = new Post_Model_Index();				
				$arr = array(
						"userid"=>$data->id,
						"title"=>strip_tags($title),
						"categoryid"=>$idcategory,
						"content"=>strip_tags($content,'<div>'),
						"status"=>$status,
						"type"=>"",
						"link"=>"",	
						"createdate"=>date('Y-m-d h:i:s',time()),										
				);
				
				$idpost = $post->insertPost($arr);
								
				$this->view->title = strip_tags($title);
				$this->view->content = $content;
				$this->view->categoryid = $idcategory;
			
				$media = new Post_Model_Media();
						
				$upload = new Zend_File_Transfer_Adapter_Http();
						
				$path = realpath(APPLICATION_PATH."/../upload/");
				
				
				$files = $upload->getFileInfo();
				
				$upload->setDestination($path);
				
				$i=1;
				
				foreach($files as $file => $info) {
					
					  $file_name = $upload->getFileName($info['name']);
					  
					  preg_match("/\.([^\.]+)$/", $file_name, $matches); 
			          $file_ext = $matches[1];
			          $file_name = $i.time() . '.' . $file_ext;
			          $upload->addFilter('Rename', $path .'/' . $file_name);
					
					if (!$upload->isValid()) 
					{
					    throw new Exception('Bad image data: '.implode(',', $upload->getMessages()));
					}
					
					try {
						
						
				        $upload->receive($file);
				        
				        $arr = array(
						"userid"=>$data->id,
						"link"=>$file_name,
						"description"=>"",
				        "postid"=>$idpost,										
						);
						
						$media->insertMedia($arr);
						
						$i++;
						    
					} 
					catch (Zend_File_Transfer_Exception $e) 
					{
					        throw new Exception('Bad image data: '.$e->getMessage());
					}
					
					
			
				}
			
				$arr = array(				
			        "postid"=>$idpost,										
				);
				
				$this->view->idpost = $idpost;
				
				$this->view->medias = $media->listItem($arr);
				
				
				
			}
			elseif ($module =="edit") {
				
				
				
				if($this->getRequest()->isPost()){

					$idpost = $_POST["idpost"];
					
					$post_model = new Post_Model_Index();
					
					$media = new Post_Model_Media();
					
					$post = $post_model->getItem($idpost);
					
					$this->view->title = $post["title"];
					$this->view->content = $post["content"];
					$this->view->categoryid = $post["categoryid"];
					
					$arr = array(				
				        "postid"=>$idpost,										
					);
					
					$this->view->idpost = $idpost;
					
					$this->view->medias = $media->listItem($arr);					
					
					$this->view->status = "preview";
					
				}
				
			}
			else{
				
				
				
				$idpost = $_POST["idpost"];
				$title = $_POST["title"];
				$content = $_POST["content"];
				$idcategory = $_POST["idcategory"];
				$status = "publish";
				
				if ($idpost){
					
					$storage = new Zend_Auth_Storage_Session();
					$data = $storage->read();
					$post = new Post_Model_Index();
					$arr = array(
							"id"=>$idpost,
							"userid"=>$data->id,						
							"title"=>strip_tags($title),
							"categoryid"=>$idcategory,
							"content"=>strip_tags($content,'<div>'),
							"status"=>$status,
							"chanelid"=>"",
							"createdate"=>date('Y-m-d h:i:s',time()),										
					);					
					$post->updatePost($arr);
					
					$this->_redirect('/post?id='.$data->id.'&pid='.$idpost);
					
						
				}
				else {
					
					$post = new Post_Model_Index();				
					$arr = array(
							"userid"=>$data->id,
							"categoryid"=>$idcategory,
							"title"=>strip_tags($title),
							"content"=>strip_tags($content,'<div>'),
							"status"=>$status,	
							"type"=>"",
							"link"=>"",
							"chanelid"=>"",				
							"createdate"=>date('Y-m-d h:i:s',time()),										
					);					
					
					$post->insertPost($arr);
					
					$this->_redirect('/post?id='.$data->id);
				}
				
				
				
			}
				
			
		}	

	if(isset($id)){
			
			$post = new Post_Model_Index();
			
			$media = new Post_Model_Media();
			
			$comment = new Post_Model_Comment();
			
			$auth = new Registration_Model_Auth();
			
			$like = new Post_Model_Like();
			
			$status = "publish";
			
			$this->view->pagecomment = 1;
			
			$this->view->pagesizecomment = 3;	
			

			$this->view->user = $auth->getItemByComment($id);
			 
			
			$page = 1;
			
			$pagesize = 8;
			
			$this->view->page = $page +1;
			
			$this->view->pagesize = $pagesize;
			
			$this->view->id = $id;
			
			$this->view->pid = $pid;
			
			$arr = array(
				"userid"=>$id,						
				"status"=>$status,	
				"page"=> $page,
				"pagesize"=> $pagesize,
																
			);
			
			if(isset($pid)){				
				$this->view->post_item = $post->getItem($pid);
				$this->view->posts =$post->listItemId($arr,$pid);
			}else{

				$this->view->posts =$post->listItem($arr);
				
			}
			
			$this->view->total = $post->countPost($arr);
			
			
			
			$this->view->media = $media;
			
			$this->view->comment = $comment;
			
			$this->view->auth = $auth;
			
			$this->view->like = $like;
			
			$category = new Category_Model_Index();
			
			$this->view->list_category = $category->listItem();
			
			$this->view->post = $post;
			$this->view->follow = new Registration_Model_Follow();
						
		}
				 		
	}
	
	public function ajaxpostAction(){
		
		$this->view->main = FALSE;
		
		$pid = $this->_request->getParam('pid');
		
		if($this->getRequest()->isPost()){
			
			$page = $_POST["page"];
			$pagesize = $_POST["pagesize"];
			$total = $_POST["total"];
			$id = $_POST["acc"];
			
			if (isset($page) && isset($pagesize) && isset($total)){
				
					$post = new Post_Model_Index();
			
					$media = new Post_Model_Media();
					
					$comment = new Post_Model_Comment();
					
					$auth = new Registration_Model_Auth();
					
					$like = new Post_Model_Like();
					
					$this->view->pagecomment = 1;
			
					$this->view->pagesizecomment = 5;			
					
					
					$status = "publish";
					
					$arr = array(
						"userid"=>$id,						
						"status"=>$status,	
						"page"=> $page,
						"pagesize"=> $pagesize,
																		
					);

					if(isset($pid)){				
						$this->view->post_item = $post->getItem($pid);
						$this->view->posts =$post->listItemId($arr,$pid);
					}else{
		
						$this->view->posts =$post->listItem($arr);
						
					}
					
					
					$this->view->media = $media;
					
					$this->view->comment = $comment;
					
					$this->view->auth = $auth;
					
					$this->view->like = $like;
						
			}
			
		}
		
		
	}
	
	public function ajaxcommentAction(){
		
		$this->view->main = FALSE;
		
		
		if($this->getRequest()->isPost()){
			
			$page = $_POST["page"];
			$pagesize = $_POST["pagesize"];
			$total = $_POST["total"];
			$idpost = $_POST["idpost"];
			
			if (isset($page) && isset($pagesize) && isset($total)){
				
					$post = new Post_Model_Index();			
					
					
					$comment = new Post_Model_Comment();
					
					$auth = new Registration_Model_Auth();
					
					
					$this->view->listcomment = $comment->listItemByPost($idpost,$page,$pagesize);
					
					
					
					$this->view->comment = $comment;
					
					$this->view->auth = $auth;
					
					
						
			}
			
		}
		
	}
	
	public function deletepostAction(){
		
		if($this->getRequest()->isPost()){
			
			$idpost = $_POST["idpost"];
			
			
			if(isset($idpost)){
				$post = new Post_Model_Index();
				$post->deletePost($idpost);
				
				
			}
			
			
			
		}
		
		
	}
	
	public function ajaxuploadAction(){		
		
		$this->view->main = FALSE;
		
		if($this->getRequest()->isPost()){			
			
			$storage = new Zend_Auth_Storage_Session();
			$data = $storage->read();
			
			$module = $_POST["module"];
			
			$this->view->module = $module;
			
			$user_wall_id = @(int)$_POST["user_wall_id"];
			
			$idchanel = @$_POST["idchanel"];
			
			if($module == "preview"){

				// Tao bai post
				$title = $_POST["title"];
				$content = $_POST["content"];
				$idcategory = $_POST["idcategory"];
				$status = "preview";
				
				$post = new Post_Model_Index();				
				$arr = array(
						"userid"=>$data->id,
						"title"=>strip_tags($title),
						"categoryid"=>$idcategory,
						"content"=>strip_tags($content),
						"status"=>$status,
						"user_wall_id"=>$user_wall_id,
						"type"=>"",
						"link"=>"",	
						"chanelid"=>$idchanel,
						"createdate"=>date('Y-m-d h:i:s',time()),										
				);
				
				$idpost = $post->insertPost($arr);
								
				$this->view->title = strip_tags($title);
				$this->view->content = $content;
				$this->view->categoryid = $idcategory;
			
				$media = new Post_Model_Media();
						
				$upload = new Zend_File_Transfer_Adapter_Http();
						
				$path = realpath(APPLICATION_PATH."/../upload/");
				
				
				$files = $upload->getFileInfo();
				
				$upload->setDestination($path);
				
				$i=1;
				
				foreach($files as $file => $info) {
					
					  $file_name = $upload->getFileName($info['name']);
					  
					  preg_match("/\.([^\.]+)$/", $file_name, $matches); 
			          $file_ext = $matches[1];
			          $file_name = $i.time() . '.' . $file_ext;
			          $upload->addFilter('Rename', $path .'/' . $file_name);
					
					if (!$upload->isValid()) 
					{
					    throw new Exception('Bad image data: '.implode(',', $upload->getMessages()));
					}
					
					try {
						
						
				        $upload->receive($file);
				        
				        $arr = array(
						"userid"=>$data->id,
						"link"=>$file_name,
						"description"=>"",
				        "postid"=>$idpost,										
						);
						
						$media->insertMedia($arr);
						
						$i++;
						    
					} 
					catch (Zend_File_Transfer_Exception $e) 
					{
					        throw new Exception('Bad image data: '.$e->getMessage());
					}
					
					
			
				}
			
				$arr = array(				
			        "postid"=>$idpost,										
				);
				
				$this->view->idpost = $idpost;
				
				$this->view->medias = $media->listItem($arr);
				
				
				
			}
			else{
				
				$idpost = @$_POST["idpost"];
				$title = @$_POST["title"];
				$content = @$_POST["content"];
				$idcategory = @$_POST["idcategory"];
				$id_youtube = @$_POST["id_youtube"];				
				$status = "publish";
				
				$str_iframe_youtube ="";
				
				if($id_youtube != ""){

					$str_iframe_youtube = '<iframe width="420" height="315" src="http://www.youtube.com/embed/'.$id_youtube.'" frameborder="0" allowfullscreen></iframe>';
					
				}
				
				$content = strip_tags($content,'<div><p><strong><ol><li><span>');
				
				$content .= $str_iframe_youtube;
				
				if ($idpost){
					
					$storage = new Zend_Auth_Storage_Session();
					$data = $storage->read();
					$post = new Post_Model_Index();
					$arr = array(
							"id"=>$idpost,
							"userid"=>$data->id,						
							"title"=>strip_tags($title),
							"categoryid"=>$idcategory,
							"content"=>$content,
							"status"=>$status,
							"user_wall_id"=>$user_wall_id,
							"chanelid"=>$idchanel,
							"createdate"=>date('Y-m-d h:i:s',time()),										
					);					
					$post->updatePost($arr);
					
					$pid = $idpost;
					
					
						
				}
				else {
					
					$post = new Post_Model_Index();				
					
					$arr = array(
							"userid"=>$data->id,
							"user_wall_id"=>$user_wall_id,
							"categoryid"=>$idcategory,
							"title"=>strip_tags($title),
							"content"=>$content,
							"status"=>$status,	
							"type"=>"",
							"link"=>"",
							"chanelid"=>$idchanel,				
							"createdate"=>date('Y-m-d h:i:s',time()),										
					);					
					
					$pid = $post->insertPost($arr);
					
				}
				
				$post = new Post_Model_Index();
			
				$media = new Post_Model_Media();
				
				$comment = new Post_Model_Comment();
				
				$auth = new Registration_Model_Auth();
				
				$like = new Post_Model_Like();

				$this->view->post_item = $post->getItem($pid);
				
				$this->view->media = $media;
		
				$this->view->comment = $comment;
				
				$this->view->auth = $auth;
				
				$this->view->like = $like;
				
				
								
			}
			
		}
		
	}
	
}